--[[
    OneCompanion, a cool companion bag for World of Warcraft
    Copyright (C) 2010 Arnaud Dovi (ad@heapoverflow.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
]]

--[[
$Rev:: 0                     $:  Revision of last commit
$Author:: arnaud             $:  Author of last commit
$Date:: 2012-09-02 22:37:24 #$:  Date of last commit
$WoW:: 5.0.5.16030           $:  Client of the last tests
]]

if not (select(4,GetBuildInfo()) >= 30000) then return end
local MINOR_VERSION = tonumber(("$Revision: 40 $"):match("%d+"))
if MINOR_VERSION > _G.OneCompanion_MINOR_VERSION then _G.OneCompanion_MINOR_VERSION = MINOR_VERSION end

OneCompanion = LibStub("AceAddon-3.0"):NewAddon("OneCompanion", "AceConsole-3.0", "AceEvent-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale("OneCompanion")
local ADB = LibStub("AceDB-3.0")
local ADBO = LibStub("AceDBOptions-3.0")
local AC = LibStub("AceConfig-3.0")
local ACD = LibStub("AceConfigDialog-3.0")

function OneCompanion:OnInitialize()
    local opt = OneCompanionOptions
    self.db = ADB:New("OneCompanionDB", opt.defaults, "Default")
    AC:RegisterOptionsTable("OneCompanion", opt.options)
--	self:SetConfigTable(opt.options)
    self.optionsFrame = ACD:AddToBlizOptions("OneCompanion", "OneCompanion")
    opt.options.args.profile = ADBO:GetOptionsTable(self.db)
    self:RegisterChatCommand("onecompanion", "OpenConfig")
    self:RegisterChatCommand("onec", "OpenConfig")
    OneCompanionFirstLoad = true
end

function OneCompanion:OnEnable()
    OneCompanionFrames.build()
    self:RegisterEvent("COMPANION_UPDATE","SoftUpdate")
    self:RegisterEvent("COMPANION_LEARNED","Update")
    self:RegisterEvent("SPELL_UPDATE_COOLDOWN","Cooldown")
end

function OneCompanion:OnDisable()
    self:UnregisterEvent("COMPANION_UPDATE")
    self:UnregisterEvent("COMPANION_LEARNED")
    self:UnregisterEvent("SPELL_UPDATE_COOLDOWN")
end

--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--

function OneCompanion:OpenConfig()
    if not ACD.OpenFrames["OneCompanion"] then
        InterfaceOptionsFrame:Hide()
        ACD:SetDefaultSize("OneCompanion", 650, 550)
        ACD:Open("OneCompanion")
    else
        InterfaceOptionsFrame:Hide()
        ACD:Close("OneCompanion")
    end
end

function OneCompanion:OpenModel()
    local self = OneCompanionFrames
    if not self.model:IsShown() then
        self.model:Show()
    else
        self.model:Hide()
    end
    self.ShowSlider()
end

function OneCompanion:SoftUpdate()
    if OneCompanionFirstLoad then
        OneCompanionFirstLoad = false
        OneCompanion:Update()
    end
end

function OneCompanion:Update()
    local self, fra, opt = OneCompanion, OneCompanionFrames, OneCompanionOptions
    opt.ToggleBackdrop(nil, nil)
    opt.ToggleShowTooltips(nil, nil, nil)
    opt.SetColor(nil, nil, nil, nil, nil)
    opt.ToggleMainFrameClamp(nil)
    opt.ToggleModelFrameClamp(nil)
    opt.ToggleShowGrip(nil)
    opt.ToggleShowModelGrip(nil)
    opt.ToggleLockBreanni(nil)
    fra.buildButtons()
    opt.ToggleShowTooltips(nil)
    opt.ToggleLockBag(nil)
    opt.ToggleButtonScale(nil, nil, nil, nil)
    opt.ToggleBreanniScale(nil, nil)
    opt.ToggleStrata(nil, nil)
    self.Cooldown()
end

function OneCompanion:Cooldown()
    local fra = OneCompanionFrames
    if fra.base:IsVisible() then
        fra:UpdateCooldowns()
    end
end

function OneCompanion:ToggleShowHide()
    local fra = OneCompanionFrames
    if fra.base:IsShown() then PlaySound("igInventoryClose") fra.base:Hide()
    else PlaySound("igInventoryOepn") fra.base:Show() end
end

--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--
